package com.navajo.web.spring;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.navajo.web.business.ApplicationUI;
import com.vaadin.server.ServiceException;
import com.vaadin.server.SessionInitEvent;
import com.vaadin.server.SessionInitListener;
import com.vaadin.server.UIClassSelectionEvent;
import com.vaadin.server.UICreateEvent;
import com.vaadin.server.UIProvider;
import com.vaadin.ui.UI;

//@Configurable
public class VaadinSpringServlet extends com.vaadin.server.VaadinServlet implements SessionInitListener {
    public VaadinSpringServlet() {
        super();

    }

    @Autowired
    private transient ApplicationContext applicationContext;



    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, servletConfig.getServletContext());
        super.init(servletConfig);
    }



    @Override
    protected void servletInitialized() {
        getService().addSessionInitListener(this);
    }



    @Override
    public void sessionInit(SessionInitEvent event) throws ServiceException {

        event.getSession().addUIProvider(new UIProvider() {
            @Override
            public UI createInstance(UICreateEvent event) {
                return (UI) applicationContext.getBean("APPLICATION");
            }



            @Override
            public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {        
                return (Class<? extends UI>) ApplicationUI.class;
            }
        });
    }



}
